package com.mo.lib.ui.activity

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.dylanc.longan.contentView
import com.mo.lib.R
import com.mo.lib.adapter.KFragmentPagerAdapter
import com.mo.lib.databinding.BaseActViewpageBinding

/**
 * @ author：mo
 * @ data：2019/3/27
 * @ 功能：TabLayout+viewpage样式的布局封装base
 */
abstract class BaseVpActivity : BaseActivity(R.layout.base_act_viewpage) {
    lateinit var viewBinding:BaseActViewpageBinding

    override fun initView(savedInstanceState: Bundle?) {
        viewBinding=  DataBindingUtil.bind(contentView)!!
        viewBinding.stlBaserVp.visibility = if (vpTitleList == null || vpTitleList?.size == 0) View.GONE else View.VISIBLE
        /**
         * 注意使用的是：getChildFragmentManager，
         * 这样setOffscreenPageLimit()就可以添加上，保留相邻3个实例，切换时不会卡
         * 但会内存溢出，在显示时加载数据
         */
        viewBinding.vpBaserVp.adapter = KFragmentPagerAdapter(supportFragmentManager, vpFragmentsList, vpTitleList)
        // 左右预加载页面的个数
        viewBinding.vpBaserVp.offscreenPageLimit = 2
        viewBinding.stlBaserVp.setViewPager(viewBinding.vpBaserVp)
        getTitleView()?.let {
            ((viewBinding.root) as LinearLayout).addView(it,0)
        }
    }
    open fun getTitleView() :View?=null
    override fun initData() {

    }

    abstract val vpFragmentsList: MutableList<Fragment>

    abstract val vpTitleList: ArrayList<String>?

    fun setSlidingEnable(slidingEnable: Boolean) {
        viewBinding.vpBaserVp.setNoScroll(slidingEnable)
    }

}